package pers.qianyu.month_202012.date_20201220;

import org.junit.*;

/**
 * 372. 超级次方
 * https://leetcode-cn.com/problems/super-pow/
 *
 * @author mizzle rain
 * @date 2020-12-20 21:45
 */
public class SuperPow {
    public int superPow(int a, int[] b) {
        return recur(a, b, b.length - 1);
    }

    private int recur(int a, int[] nums, int index) {
        if (index < 0) {
            return 1;
        }
        int num = nums[index];
        int res1 = pow(recur(a, nums, index - 1), 10);
        int res2 = pow(a, num);
        return res1 * res2 % 1337;
    }

    private int pow(int a, int n) {
        if (n == 0) {
            return 1;
        }
        a %= 1337;
        int res = a;
        for (int i = 0; i < n - 1; i++) {
            res *= a;
            res %= 1337;
        }
        return res;
    }

    @Test
    public void test1() {
        int a = 2;
        int[] b = {1, 0};
        int result = new SuperPow().superPow(a, b);
        Assert.assertEquals(result, 1024);
    }
}
